<?php


namespace App\Http\Controllers\Api\Orders;


use App\Enum\OrderEnum;
use App\Exports\OrderExport;
use App\Exports\WithdrawExport;
use App\Http\Controllers\Controller;
use App\Models\CompanyManagement;
use App\Models\OrderDistance;
use App\Repositories\OrderRepository;
use App\Transforms\DriverTransformer;
use App\Transforms\OrderTransformer;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;


class OrderController extends Controller
{

    /**
     * 订单分页
     * @param Request $request
     * @param OrderRepository $repository
     * @return OrderController|JsonResponse
     * @Author 姿势就是力量
     */
    public function getPageList(Request $request, OrderRepository $repository)
    {

        $type = $request->get('type', '0'); // 默认快车订单
        $types = (string)$request->get('types', '0'); // 默认快车订单 0 快车  1 代驾
        $passenger=$request->get('u_phone','');//查询用户手机号
        $companyId = (int)$request->get('company_id', '4');
        $driverId = (int)$request->get('driver_id', '');
        $userId = (int)$request->get('user_id', '');
        $pageSize = (int)$request->get('page_size', 10);
        $order_sn=(string)$request->get('order_sn',''); //查询订单号
        $number=(string)$request->get('number','');//查询车牌号
        $reality_start=(string)$request->get('reality_start','');//查询实际起始地
        $reality_end=(string)$request->get('reality_end','');//查询实际终点
        $type_name=(int)$request->get('name','');//查询业务类型
        $created_at=(string)$request->get('created_at','');//查询下单时间
        $book_time=(string)$request->get('book_time','');//查询预约用车时间
        $end_time=(string)$request->get('end_time','');//查询结束时间
        $reality_start_time=(string)$request->get('reality_start_time','');//查询实际上车时间
        $should_price=(string)$request->get('should_price','');//查询应收金额
        $discount_price=(string)$request->get('discount_price','');//查询优惠金额
        $km=(string)$request->get('km','');//查询服务里程
        $minute=(string)$request->get('minute','');//查询服务总服务时间
        $c_name=(string)$request->get('c_name','');//查询服务机构
        $nick_name=(string)$request->get('nick_name','');//查询用户昵称
        $mobile=(string)$request->get('d_phone','');//查询司机电话
        $real_name=(string)$request->get('real_name','');//查询司机名称
        $driver_money=(string)$request->get('driver_money','');//查询司机应得
        $surplus_profit=(string)$request->get('surplus_profit','');//查询剩余利润
        $invoice_number=(string)$request->get('invoice_number','');//查询发票号
        $cancel_reason=(string)$request->get('cancel_reason','');//撤单理由
        $status=(int)$request->get('status','');//订单状态
        $list = $repository->getPageList($companyId,$types,$status, $type_name,$type,$passenger, $order_sn,$number,
            $reality_start,$reality_end,$created_at,$book_time,$end_time,
            $reality_start_time,$should_price,$discount_price,$km,$minute,$c_name,
            $nick_name,$mobile,$real_name,$driver_money, $surplus_profit,$invoice_number,
            $cancel_reason,
            $pageSize);
        // 司机统计信息
        if ($driverId) {
            $list['driver_count'] = $repository->countBy(['driver_id' => $driverId]);
            $list['driver_service_money'] = $repository->sumBy(['driver_id' => $driverId], 'real_price');
            $list['driver_due_money'] = $repository->sumBy(['driver_id' => $driverId], 'should_price');
        }

        // 乘客统计信息
        if ($userId) {
            $list['passenger_count'] = $repository->countBy(['user_id' => $userId]);
            $list['passenger_money'] = $repository->sumBy(['user_id' => $userId], 'real_price');
        }

        return $this->sendJson($list);
    }



    /**
     * 订单导出
     * @param Request $request
     * @param OrderRepository $repository
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     * @Author 姿势就是力量
     */
    public function orderExcel (Request $request, OrderRepository $repository)
    {
        $user_id = $request->get('management_id','');  //查所属公司
        $types = $request->get('types','0');  //订单类型  0 网约车订单  1 代驾订单
        $company = (new CompanyManagement())->where('management_id',$user_id)->get()->toArray();  //查所属公司
        $companyIds =array_column($company, 'company_id');   //查所属公司
        $type = $request->get('type', ''); // 默认快车订单
        $passenger=$request->get('u_phone','');//查询用户手机号
        $driverId = (int)$request->get('driver_id', '');
        $userId = (int)$request->get('user_id', '');
        $order_sn=(string)$request->get('order_sn',''); //查询订单号
        $number=(string)$request->get('number','');//查询车牌号
        $reality_start=(string)$request->get('reality_start','');//查询实际起始地
        $reality_end=(string)$request->get('reality_end','');//查询实际终点
        $type_name=(int)$request->get('name','');//查询业务类型
        $created_at=(string)$request->get('created_at','');//查询下单时间
        $book_time=(string)$request->get('book_time','');//查询预约用车时间
        $end_time=(string)$request->get('end_time','');//查询结束时间
        $reality_start_time=(string)$request->get('reality_start_time','');//查询实际上车时间
        $should_price=(string)$request->get('should_price','');//查询应收金额
        $discount_price=(string)$request->get('discount_price','');//查询优惠金额
        $km=(string)$request->get('km','');//查询服务里程
        $minute=(string)$request->get('minute','');//查询服务总服务时间
        $c_name=(string)$request->get('c_name','');//查询服务机构
        $nick_name=(string)$request->get('nick_name','');//查询用户昵称
        $mobile=(string)$request->get('d_phone','');//查询司机电话
        $real_name=(string)$request->get('real_name','');//查询司机名称
        $driver_money=(string)$request->get('driver_money','');//查询司机应得
        $surplus_profit=(string)$request->get('surplus_profit','');//查询剩余利润
        $invoice_number=(string)$request->get('invoice_number','');//查询发票号
        $cancel_reason=(string)$request->get('cancel_reason','');//撤单理由
        $status=(int)$request->get('status','');//订单状态
        $list = $repository->orderExcel($companyIds,$status, $type_name,$type,$passenger, $order_sn,$number,
            $reality_start,$reality_end,$created_at,$book_time,$end_time,
            $reality_start_time,$should_price,$discount_price,$km,$minute,$c_name,
            $nick_name,$mobile,$real_name,$driver_money, $surplus_profit,$invoice_number,
            $cancel_reason,$types);
        $list['data'] = (new OrderTransformer())->transformList($list);
        $order = $list['data'];
        $row = [
            [
                "id" => 'ID',
                "status" => '订单状态',
                "order_sn" => '订单号',
                "book_time" => '预约用车时间',
                "reality_start_time" => '实际上车时间',
                "reality_start" => '实际起始地',
                "reality_end" => '实际目的地',
                "created_at" => '下单时间',
                "end_time" => '结束时间',
                "name" => '业务类型',
                "real_should_price" => '应收金额',
                "coupon_price" => '优惠金额',
                "real_price" => '实收金额',
                "km" => '总服务里程',
                "minute" => '总服务时间',
                "copany_name" => '服务机构',
                "user_name" => '下单用户',
                "user_phone" => '乘客电话',
                "real_name" => '司机名称',
                "phone" => '司机电话',
                "number" => '车牌号',
                "source" => '订单来源',
                "driver_money" => '司机应得',
                "surplus_profit" => '剩余利润',
                "cancel_reason" => '撤单理由',
                "invoice_number" => '发票号',
                "remark" => '备注',
            ]
        ];
        return Excel::download(new OrderExport($row,$order), '订单.xls');

    }


    /**
     * 订单详情
     * @param Request $request
     * @param OrderRepository $repository
     * @return OrderController|JsonResponse
     * @Author 姿势就是力量
     */
    public function getFirst(Request $request, OrderRepository $repository)
    {
        $orderId = (int)$request->get('order_id', '');
        $order = $repository->getFind($orderId);
      //  $order['total'] = $order->toll_price+$order->stop_price;
        return $this->sendJson($order);
    }


    /**
     * 保存订单信息
     * @param Request $request
     * @param OrderRepository $repository
     * @return OrderController|JsonResponse
     * @Author 姿势就是力量
     */
    public function save(Request $request, OrderRepository $repository)
    {
        $data = [];
        $data['id'] = $request->post('id', '');
        $request->has('remark') ? $data['remark'] = $request->post('remark') : false;
        $request->has('status') ? $data['status'] = OrderEnum::CANCEL_ADMIN : false;
        $request->has('cancel_reason') ? $data['cancel_reason'] = $request->post('cancel_reason') : false;

        $order = $repository->saveData($data);
        if (!$order) return $this->sendError('保存失败');
        return $this->sendSuccess('保存成功');
    }


}
